package com.haoran.dwz.common.http;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.haoran.dwz.common.util.HttpClientUtil;


@RequestMapping("/api")
@Configuration
public class HttpClientController {
	protected Logger logger =LoggerFactory.getLogger(getClass());
	
	@Value("${kye.openapi.access.address}")
	private String accessAddress="http://172.20.2.57:8080/kyeopenapi/";
	@Value("${kye.openapi.access.code}")
	private String accessCode="10005";
	@Value("${kye.openapi.access.key}")
	private String accessKey="A63F2C3FF29A606979AE9D25F737B342";
 
	/**
	 * 创建跨越的客户请求
	 * @return
	 */
	private KyeHttpClientRequest buildKyeHttpClientRequest(){
		return   KyeHttpClientRequest.build(accessAddress, accessCode, accessKey);
	}
	
	/**
	 * 向API发送post请求
	 * @param api
	 * @param json
	 * @return
	 */
	protected JSONObject doPost(String api, String json) {
		HttpClientRequest httpClientRequest = buildKyeHttpClientRequest().build(api,json);
		HttpClientResponse response = HttpClientUtil.doPost(httpClientRequest);
        String loginResult = response.getBody();
        JSONObject loginObj = JSONObject.parseObject(loginResult);
		return loginObj;
	}
	
	/**
	 * 向API发送post请求
	 * @param api
	 * @param json
	 * @return
	 */
	protected Object doPost(String api, Map<String,Object> param) {
		HttpClientRequest httpClientRequest = buildKyeHttpClientRequest().build(api,param);
		HttpClientResponse response = HttpClientUtil.doPost(httpClientRequest);
        String loginResult = response.getBody();
        Object loginObj = JSONObject.parse(loginResult);
		return loginObj;
	}
	
}
